package Cau12;

import java.awt.EventQueue;
import java.awt.FileDialog;
import java.awt.Frame;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class ClientMahoa implements ActionListener {

	private JFrame frmClientuploadsinhcoi;
	private JTextField txtpath;
	JButton btnUpload;
	JButton btnReset;
	String strname;
	private JLabel lbtrangthai;
	byte[] arrByteOfSentFile = null;
	byte[]arrByteofReciever=null;
	private FileOutputStream fos;
	private FileInputStream fin;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientMahoa window = new ClientMahoa();
					window.frmClientuploadsinhcoi.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ClientMahoa() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmClientuploadsinhcoi = new JFrame();
		frmClientuploadsinhcoi.setTitle("Client_CryptographyFile_Sinh_coi");
		frmClientuploadsinhcoi.setBounds(100, 100, 450, 152);
		frmClientuploadsinhcoi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmClientuploadsinhcoi.getContentPane().setLayout(null);
		
		JLabel lblFileUpload = new JLabel("File Upload:");
		lblFileUpload.setBounds(10, 11, 87, 14);
		frmClientuploadsinhcoi.getContentPane().add(lblFileUpload);
		
		txtpath = new JTextField();
		txtpath.setBounds(10, 28, 369, 20);
		frmClientuploadsinhcoi.getContentPane().add(txtpath);
		txtpath.setColumns(10);
		
		JButton btnBrowes = new JButton("Browes");
		btnBrowes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtpath.setText(showDialog());
				lbtrangthai.setText(strname+"  chuan bi duoc up!");
				btnReset.setEnabled(true);
				btnUpload.setEnabled(true);
			}
		});
		btnBrowes.setBounds(379, 28, 45, 21);
		frmClientuploadsinhcoi.getContentPane().add(btnBrowes);
		
		btnUpload = new JButton("Crytography");
		btnUpload.setEnabled(false);
		btnUpload.setBounds(100, 59, 110, 23);
		btnUpload.addActionListener(this);
		frmClientuploadsinhcoi.getContentPane().add(btnUpload);
		
		btnReset = new JButton("Reset");
		btnReset.setEnabled(false);
		btnReset.setBounds(271, 59, 89, 23);
		btnReset.addActionListener(this);
		frmClientuploadsinhcoi.getContentPane().add(btnReset);
		
		lbtrangthai = new JLabel("Chon file rui nhan Upload");
		lbtrangthai.setBounds(78, 93, 301, 14);
		frmClientuploadsinhcoi.getContentPane().add(lbtrangthai);
	}
	//ham chon duong dan de save file tra ve.
	public static String SaveFile(String path)
	{
	     JFileChooser fc = new JFileChooser ();
	     File fFile = new File (path);
	     fc.setCurrentDirectory (new File ("."));
	     fc.setAcceptAllFileFilterUsed(true);
	     fc.setSelectedFile (fFile);
	     int result = fc.showSaveDialog (fc);
	     if (result == JFileChooser.CANCEL_OPTION) {
	         return null;
	     } else if (result == JFileChooser.APPROVE_OPTION) {
	         fFile = fc.getSelectedFile ();
	         if (fFile.exists ()) {
	             int response = JOptionPane.showConfirmDialog (null,
	               "Overwrite existing file?","Confirm Overwrite",
	                JOptionPane.OK_CANCEL_OPTION,
	                JOptionPane.QUESTION_MESSAGE);
	             if (response == JOptionPane.CANCEL_OPTION) return null;
	         }
	         return fFile.toString();
	     } else {
	       return null;
	     }
	}
	public String showDialog() {
		FileDialog fd = new FileDialog(new Frame(), "Select File...",
				FileDialog.LOAD);
		fd.show();
		strname=fd.getFile();
		//System.out.print(strname);
		return fd.getDirectory() + fd.getFile();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if(arg0.getSource()==btnUpload)
		{			
			try {
				Imahoa iup=(Imahoa)Naming.lookup("rmi://localhost/Servermahoa");
				System.out.println("Da tim thay doi tuong tren server...");				
				fin = new FileInputStream(txtpath.getText());
				int size=fin.available();
				arrByteOfSentFile=new byte[size];
				fin.read(arrByteOfSentFile, 0, size);
				arrByteofReciever=(byte[])iup.XuLyFile(arrByteOfSentFile);
				String temp=SaveFile("CryptographyFile "+strname);
				String pathfile;
				if(temp==null) pathfile="CryptographyFile"+strname;
				else pathfile=temp;				
				fos=new FileOutputStream(pathfile);
				fos.write(arrByteofReciever);
				fos.flush();
				System.out.println("Upload file thanh cong");				
				fin.close();
				fos.close();
				lbtrangthai.setText("CryptographyFile complete!");				
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NotBoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(arg0.getSource()==btnReset)
		{
			txtpath.setText("");
			btnUpload.setEnabled(false);
			btnUpload.setEnabled(false);
		}
		
	}

}
